﻿using HBase;
using hCommand;
using Net.http;
using System;
using System.Collections.Generic;
using System.Reflection.Emit;

namespace Persistence {
    public class PkFinshEngine : GkTransaction {
        public long pkId;


        protected override TransRes Execute() {

            WhDopk pk = getById<WhDopk>(pkId);
            if (pk.status != 2) {
                return new TransRes("盘库计划状态异常");
            }
            string selectSql = "select t.box_code from core_stock s join core_task t on  s.id=t.stock_id  where PK_ID=@pkId  and t.status<>{0} and t.status<>{1}";
            List<String> boxCodes = query<String>(HUtil.s(selectSql, CoreTaskStatus.结束, CoreTaskStatus.删除), new { pkId });
            if (boxCodes.Count > 0) { 
                return new TransRes(ListUtil.info("有托盘未完成,托盘号",boxCodes));
            }

            String query = "Select * from  core_stock_det  where PK_ID=@pkId  and status in ({0},{1})";
            List<CoreStockDet> List = query<CoreStockDet>(HUtil.s(query, StockDetailStatus.盘亏中, StockDetailStatus.盘盈), new { pkId });

           
            updateDet(pkId);
            statPkRes(pkId);
            updateStock(pkId);
            StatUtil.changeBatch(this,List );
     
            exec("update Wh_Dopk set status=" + (int)DoStatus.执行完成 + " where id=@id", pk);
            return new TransRes();
        }

        void statPkRes(long pkId) {

            String sql = @"SELECT  t.item_Id ,t.batch_No,sum(CASE when t.doi_Det_Id<>@pkId then t.count_db else 0  end ) old, 
                         sum( CASE when t.status=9 then t.count_db else 0  end ) now
                        FROM core_stock_det t where t.pk_id=@pkId   group by t.item_Id,t.batch_No";
            List<WhDopkRes> resList = query<WhDopkRes>(sql, new { pkId });
            foreach (WhDopkRes res in resList) {
                res.pkId = pkId;
            }
            if (resList.Count > 0) {
                batchInsert(resList);
            }

        }

       void  updateDet(long pkId) {//执行顺序不能换
            string sql = "update core_stock_det set status={0} ,doo_Type={2},doo_Det_Id=@pkId,out_time=sgj_get_sys_now  where PK_ID=@pkId  and (status={1} or   count_db=0) ";
              exec(HUtil.s(sql, StockDetailStatus.单据盘亏出库, StockDetailStatus.盘亏中, DetConst.DooTypePk), new { pkId });
            
            string sql2 = "update core_stock_det set status={0},doi_Type={2},doi_Det_Id=@pkId,create_time=sgj_get_sys_now where PK_ID=@pkId  and status={1}";
            exec(HUtil.s(sql2, StockDetailStatus.库存, StockDetailStatus.盘盈, DetConst.DoiTypePy), new { pkId });

      
        
        }
        


        public void updateStock (long pkId) {
            string stockSql = " update core_stock t set status={0},d_count =IFNULL( (select sum(count_db) from core_stock_det d  where d.pid = t.id and status={1}),0) where PK_ID=@pkId  ";
            exec(HUtil.s(stockSql, StockStatus.库存, StockDetailStatus.库存), new { pkId });

            string stockSql2 = " update core_stock t set status={0}  where PK_ID=@pkId   and d_count > 0 and loc_id<100 ";
            exec(HUtil.s(stockSql2, StockStatus.库外), new { pkId });

            string outStockSql = " update core_stock t set status={0} where PK_ID=@pkId  and d_count = 0  and loc_id<100";
            exec(HUtil.s(outStockSql, StockStatus.盘亏删除, StockStatus.库外), new { pkId });
        }
       
         
    }
}
